package com.hnrd.infomation_collerction.config;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpMethod;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author oktfolio
 * @date 2018/09/14
 */
@Configuration
@Order(1)
public class CorsConfig {

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                // 添加映射路径
                registry.addMapping("/**")
                        // 允许哪些原始域
                        .allowedOrigins("*")
                        // 是否发送 Cookie 信息
                        .allowCredentials(true)
                        // 允许哪些请求方法
                        .allowedMethods(HttpMethod.DELETE.name(), HttpMethod.POST.name(),
                                HttpMethod.PUT.name(), HttpMethod.PATCH.name(),
                                HttpMethod.GET.name(), HttpMethod.OPTIONS.name())
                        // 允许哪些头信息
                        .allowedHeaders("*");
            }
        };
    }
    /*private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 1
        corsConfiguration.addAllowedHeader("*"); // 2
        corsConfiguration.addAllowedMethod("*"); // 3
        return corsConfiguration;
    }

    
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/manager/**", buildConfig());
        return new CorsFilter(source);
    }*/
    
}
